<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <!-- 第一行 -->
        <!-- 清空就重新调用一下接口就好  -->
        <el-col :span="12">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <!--  -->
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!--  -->

      <!-- 表单 -->
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="名称" width="550"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80px"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80px"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 20, 30]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>
    <!-- 添加商品的对话框 -->
    <el-dialog title="添加商品" :visible.sync="addDialogVisible" width="50%">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        //商品列表的请求参数
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      goodsList: [], //商品列表
      total: 0, //商品总数
      addDialogVisible: false //控制添加商品对话框的按钮
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品数据列表
    async getGoodsList() {
      var { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error("获取商品数据列表失败")
      console.log(res)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 分页的一页显示的条数
    handleSizeChange(size) {
      this.queryInfo.pagesize = size
      this.getGoodsList()
    },
    // 分页的当前页发生变化
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num
      this.getGoodsList()
    },

    // 删除商品
    async removeGoods(scope) {
      var result = await this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(error => error)
      console.log(result)
      if (result !== "confirm") return
      var { data: res } = await this.$http.delete("goods/" + scope.goods_id)
      if (res.meta.status !== 200) return this.$message.error("删除商品失败")
      this.$message.success("删除商品成功")
      this.getGoodsList()
    },
    // 添加商品
    addGoods() {
      this.$router.push("/goods/add")
    }
  }
}
</script>
<style scoped></style>
